package com.ymtms.customerservice.service.serviceimpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ymtms.customerservice.dao.ComplaintInformationMapper;
import com.ymtms.customerservice.pojo.ComplaintInformation;
import com.ymtms.customerservice.service.ComplaintInformationService;
import com.ymtms.customerservice.util.TimeRangeUtil;
import com.ymtms.customerservice.vo.GridRequest;
import com.ymtms.customerservice.vo.GridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class ComplaintInformationServiceImpl implements ComplaintInformationService {
    @Autowired
    ComplaintInformationMapper complaintInformationMapper;


    @Override
    public List<ComplaintInformation> GetAllComplaintInformation(String customer,String goodsBillCode,String appealDate,String ifHandle) {
        List<ComplaintInformation> complaintInformations =complaintInformationMapper.selectAll(customer,goodsBillCode,appealDate,ifHandle);
        System.out.println(complaintInformations);
        return complaintInformations;
    }

    @Override
    public String insertComplaint(String customer, String goodsBillCode, String appealContent, String appealDate) {
        try {
            complaintInformationMapper.insertComplaint(customer,goodsBillCode,appealContent,appealDate);
            System.out.println(customer);
            return "添加投诉成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "添加投诉失败";
        }

    }

    @Override
    public List<String> findAllGoodsBillCodeByCustomerNO(String customerNO) {
        List<String> codes = complaintInformationMapper.selectGoodsBillCodeByCustomerNO(customerNO);
        for (String code : codes) {
            System.out.println(code);
        }
        return codes;
    }

    @Override
    public String refundComplainInformationByCiid(Integer ciid,String dealPerson,String dealDate) {
        try {
            complaintInformationMapper.refund(ciid,dealPerson,dealDate);
            return "处理成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "处理失败";
        }
    }

    @Override
    public String ignoreComplainInformationByCiid(Integer ciid, String dealPerson, String dealDate) {
        try {
            complaintInformationMapper.ignore(ciid,dealPerson,dealDate);
            return "处理成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "处理失败";
        }
    }

    @Override
    public GridResult<ComplaintInformation> queryByPage(GridRequest gridRequest, ComplaintInformation complaintInformation,
                                                        String sappealDate,TimeRangeUtil timeRangeUtil) {
        // 获取当前页和页大小
        Integer currentPage = gridRequest.getPage();
        Integer pageSize = gridRequest.getLimit();

        // 判断是否有排序,有则生成符合pagehelper的排序字符串
        String sidx = gridRequest.getSidx(); // 排序参数
        String sord = gridRequest.getSord(); // 排序规则

        System.out.println(currentPage);
        System.out.println(pageSize);
        System.out.println(sidx);
        System.out.println(sord);

        String orderBy = "";
        // pagehelper静态设置页属性方法，它会拦截下面的第一个查询！第二个无效。
        if(checkOrderBy(sidx, sord)){
            sidx = upperCharToUnderLine(sidx);
            orderBy = sidx+" "+sord;

            System.out.println(orderBy);

            PageHelper.startPage(currentPage, pageSize, orderBy);
        }else{
            // 设置当前页和页大小
            PageHelper.startPage(currentPage, pageSize);
        }


        // 查询结果（传入排序）
        List<ComplaintInformation> result = complaintInformationMapper.queryByPage(complaintInformation,sappealDate, timeRangeUtil);

        // 利用当前页和页大小配置pageinfo
        PageInfo<ComplaintInformation> pageList = new PageInfo<>(result);
        // 新建一个封装类对象，设置参数，传入查询得到的数据
        GridResult<ComplaintInformation> grid = new GridResult<>();

        // 存入查询数据
        grid.setData(result);
        // 存入总条数
        grid.setCount(pageList.getTotal());

        return grid;
    }

    @Override
    public String delete(Integer ciid, String dealPerson, String dealDate) {
        try {
            complaintInformationMapper.delete(ciid,dealPerson,dealDate);
            return "删除成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "删除失败";
        }
    }

    // 判断是否非空
    private boolean checkOrderBy(String sidx,String sord){
        if(sidx != null && sord != null
                && sidx.trim().length() > 0
                && sord.trim().length() > 0){
            return true;
        }
        return false;
    }
    // 大写转换为下划线+小写
    public static String upperCharToUnderLine(String param) {
        Pattern p = Pattern.compile("[A-Z]");
        if (param == null || param.equals("")) {
            return "";
        }
        StringBuilder builder = new StringBuilder(param);
        Matcher mc = p.matcher(param);
        int i = 0;
        while (mc.find()) {
            System.out.println(builder.toString());
            System.out.println("mc.start():" + mc.start() + ", i: " + i);
            System.out.println("mc.end():" + mc.start() + ", i: " + i);
            builder.replace(mc.start() + i, mc.end() + i, "_" + mc.group().toLowerCase());
            i++;
        }

        if ('_' == builder.charAt(0)) {
            builder.deleteCharAt(0);
        }
        return builder.toString();
    }
}
